package Code.Final.StudentManagementSystem;

import java.util.ArrayList;
import java.util.Scanner;


public class StudentSystem {

    static ArrayList<Student> list = new ArrayList<>();  //学生类的集合

    static {
        //静态代码块
        list.add(new Student("001", "王恒", 24, "红安"));
    }

    private static final String ADD_STUDENT = "1";
    private static final String DELETE_STUDENT = "2";
    private static final String UPDATE_STUDENT = "3";
    private static final String QUERY_STUDENT = "4";
    private static final String SHOW_STUDENT = "5";
    private static final String EXIT_STUDENT = "6";
    public static void StartStudentSystem() {

        System.out.println("----------欢-迎-进-入-学-生-管-理-系-统----------");
        loop:
        while (true) { //循环操作
            System.out.println("1：添加学生\n2：删除学生\n3：修改学生\n4：查询学生\n5：展示\n6：退出");
            System.out.print("请输入选项：");
            String choose = Print();
            switch (choose) {
                case ADD_STUDENT -> addStudent(list);
                case DELETE_STUDENT -> deleteStudent(list);
                case UPDATE_STUDENT -> updateStudent(list);
                case QUERY_STUDENT-> queryStudent(list);
                case SHOW_STUDENT -> OutputFormat(list);
                case EXIT_STUDENT -> {
                    System.out.println("退出");
                    break loop; //第一种跳出外层循环的方法 ：指定跳出的循环
                    //System.exit(0); //第二种跳出外层循环的方法：停止虚拟机运行
                }
                default -> System.out.println("没有这个选项");
            }
        }

    }

    //添加学生
    public static void addStudent(ArrayList<Student> list) {
        Student student = new Student();
        while (true) {
            System.out.println("请输入学生的id：");
            student.setId(Print());
            if (find(list, student.getId())) {
                System.out.println("id已存在，请重新输入");
            } else {
                break;
            }
        }
        System.out.println("请输入学生的姓名：");
        student.setName(Print());
        while (true){
            System.out.println("请输入学生的年龄：");
            Scanner scanner = new Scanner(System.in);
            student.setAge(scanner.nextInt());
            if (student.getAge() != 0) {
                break;
            }
        }
        System.out.println("请输入家庭住址：");
        student.setAddress(Print());


        list.add(student);
        System.out.println("添加成功！");

    }

    //删除学生
    public static void deleteStudent(ArrayList<Student> list) {
        System.out.println("请输入删除学生的id：");
        String id = Print();
        if (find(list, id)) {
            int index = studentIndex(list, id);
            list.remove(index);
            System.out.println("删除成功！");
        } else {
            System.out.println("该id不存在，请重新操作");
        }
    }

    //修改学生
    public static void updateStudent(ArrayList<Student> list) {
        System.out.println("请输入修改学生的id：");
        String id = Print();
        if (find(list, id)) {
            int index = studentIndex(list, id);
            System.out.println("请输入修改的姓名：");
            list.get(index).setName(Print());
            Scanner scanner = new Scanner(System.in);
            while (true){
                System.out.println("请输入修改的年龄：");
                int age = scanner.nextInt();
                if (age > 0 && age < 100) {
                    list.get(index).setAge(age);
                    break;
                } else {
                    System.out.println("年龄输入错误，请重新输入");
                }
            }
            System.out.println("请输入修改的家庭地址：");
            list.get(index).setAddress(Print());
            System.out.println("修改成功！");
        } else {
            System.out.println("id不存在，请重新操作");
        }

    }

    //查询学生
    public static void queryStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("当前无学生信息，请添加后查询");
            return;
        }

        System.out.println("请输入要查找的id：");
        String id = Print();

        if (find(list, id)) {
            int index = studentIndex(list, id);
            ArrayList<Student> newList = new ArrayList<>();
            newList.add(list.get(index));
            OutputFormat(newList);

        } else {
            System.out.println("该id不存在，请重新操作");
        }
    }

    //查找id是否存在
    public static boolean find(ArrayList<Student> list, String id) {
        return studentIndex(list, id) >= 0;
    }

    //通过id来找索引
    public static int studentIndex(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {  //遍历集合的每个元素
            String studentID = list.get(i).getId();  //获取元素的id
            if (studentID.equals(id)) {  //拿元素的id和输入的id进行对比
                return i;  //存在返回索引
            }
        }
        return -1;  //不存在返回-1
    }

    //键盘输入
    public static String Print() {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next(); //不管键盘录入什么都能接收
        return str;
    }

    //输出格式
    public static void OutputFormat(ArrayList<Student> list) {
        System.out.println();
        System.out.println("id\t\t姓名\t年龄\t家庭住址");
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            System.out.println(s.getId() + "\t\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getAddress());
        }
        System.out.println();
    }
}
